var datePickerName = "datePickerName";
var startYear = new Date().getFullYear();
var endYear = 1930;
var shortMonth = false;
var monthsArray = new Array();
var monthsValues = new Array();
var monthsMaxDates = new Array();
var monthMaxDateHash = new Array();
var dateString;

function initMonthMaxDateHash() {
	monthsArray = ["Month","January","February","March","April","May","June","July","August","September","October","November","December"];
	monthsValues = ["","01","02","03","04","05","06","07","08","09","10","11","12"];
	monthsMaxDates = [0,31,28,31,30,31,30,31,31,30,31,30,31];
	monthMaxDateHash = [monthsArray, monthsValues, monthsMaxDates];
}
function getMonthMaxDateHash() {
	return monthMaxDateHash;
}

function newDatePicker(datePickerName_, startYear_, endYear_, shortMonth_) {
	datePickerName = datePickerName_;
	startYear = startYear_;
	endYear = endYear_;
	shortMonth = shortMonth_;
	initialize();
}
function initialize() {
	document.write("<div id=\"div" + datePickerName + "\"></div>");
	initMonthMaxDateHash();
	var div = document.getElementById("div"+ datePickerName);
	div.appendChild(newSelectMonth());
	div.appendChild(newSelectDate());
	div.appendChild(newSelectYear());	
	//initDateValue();
}
function addOption(selObj, text, value) {
	var option = document.createElement("option");
	option.text = text;
	option.value = value;
	try {
           selObj.add(option, null);
   }
   catch (ex) {
           selObj.add(option);
   }        
}
function newSelectMonth() {
	var selMonth = document.createElement("select");
	for (var i=0; i<monthsArray.length; i++) {
		if (!shortMonth) {
			addOption(selMonth, monthsArray[i], monthsValues[i]);		
		}
		else {
			addOption(selMonth, monthsValues[i], monthsValues[i]);		
		}
	}
	selMonth.id = datePickerName + "Month";
	selMonth.onchange = onMonthChange;
	return selMonth;
}
function newSelectDate() {
	var j = 31;
	var yyyy = 0;	
	try {
		j = monthsMaxDates[document.getElementById(datePickerName + "Month").selectedIndex];
		var selYear = document.getElementById(datePickerName + "Year");
		yyyy = selYear.options[selYear.selectedIndex].value;
		if (j==28 && isLeapYear(yyyy)) {
			j=29;
		}	
	}
	catch (ex) {
	}	
	
	var selDate = document.createElement("select");
	addOption(selDate,"Day", "");
	for (var i=1; i<=j; i++) {
		var i_ = (i<10) ? "0" + i.toString(): i.toString(); 
		addOption(selDate, i_, i_);
	}	
	selDate.id = datePickerName + "Date";
	
	return selDate;
}
function newSelectYear() {
	var selYear = document.createElement("select");
	addOption(selYear,"Year", "");
	if (startYear>endYear) {
		for (var i=startYear; i>=endYear; i--) {
			addOption(selYear, i, i);
		}
	}
	else {
		for (var i=starYear; i<=endYear; i++) {
			addOption(selYear, i, i);
		}
	}
	selYear.id = datePickerName + "Year";
	selYear.onchange = onYearChange;
	return selYear;
}

function onMonthChange() {
	var selMonth = document.getElementById(datePickerName + "Month");
	var selDate = document.getElementById(datePickerName + "Date");
	var i = monthsMaxDates[selMonth.selectedIndex];
	var j = selDate.length;
	var d = selDate.selectedIndex;
	replaceSelObject(selDate, newSelectDate());
	if (d<selDate.length) {
		selDate.selectedIndex = d;
	}
	setDateValue();	
}
function onYearChange() {
	var selYear = document.getElementById(datePickerName + "Year");
	var yyyy = selYear.options[selYear.selectedIndex].value;		
	onMonthChange();
}

function replaceSelObject(selObj, selObj1) {
	for (var i=selObj.length - 1; i>=0; i--) {
		selObj.options[i] = null;
	}
		
	for (var i=0; i<selObj1.length; i++) {
		addOption(selObj, selObj1.options[i].text, selObj1.options[i].value);
	}
}
function isLeapYear(yyyy) {
	return new Date(yyyy,2-1,29).getDate()==29;
}
function getDateString() {
	var selMonth = document.getElementById(datePickerName + "Month");
	var selDate = document.getElementById(datePickerName + "Date");
	var selYear = document.getElementById(datePickerName + "Year");
	var MM = selMonth.options[selMonth.selectedIndex].value;
	var dd = selDate.options[selDate.selectedIndex].value;
	var yyyy = selYear.options[selYear.selectedIndex].value;
	
	if (MM=="" || dd=="" || yyyy=="") {
		return "";
	}
	dateString = MM + "-" + dd + "-" + yyyy;
	return dateString;
}
function setDateValue() {
	var input = document.getElementById(datePickerName);
	input.value = getDateString();
}
function initDateValue() {
	var input;
	var dateString;
	var MM = "Month";
	var dd = "Day";
	var yyyy = "Year";
	try {
		input = document.getElementById(datePickerName);
	}
	catch (ex) {
	}
	if (input==null) {
		return;
	}
	dateString = input.value;
	alert(dateString);
	var str = dateString.split("-");
	MM = str[0];
	dd = str[1];
	yyyy = str[2];
	var selMonth = document.getElementById(datePickerName + "Month");
	var selDate = document.getElementById(datePickerName + "Date");
	var selYear = document.getElementById(datePickerName + "Year");	
	setSelectValue(selMonth, MM);
	setSelectValue(selDate, dd);
	setSelectValue(selYear, yyyy);
}
function setSelectValue(selObj, value) {
	var j = 0;
	for (var i=0; i<selObj.length; i++) {
		if (selObj.options[i]==value) {
			j=i;
			break;
		}
	}
	selObj.selectedIndex = j;
}

